﻿using System.Runtime.CompilerServices;

namespace YTLog
{
    public class Logger
    {
        #region 日志简便使用

        public static LoggerWrapper Main { get; set; } = new LoggerWrapper(LogType.LogMain);

        public static LoggerWrapper Net { get; set; } = new LoggerWrapper(LogType.LogNet);

        public static LoggerWrapper Device { get; set; } = new LoggerWrapper(LogType.LogDevice);

        public static LoggerWrapper Cash { get; set; } = new LoggerWrapper(LogType.LogCash);

        public static LoggerWrapper POS { get; set; } = new LoggerWrapper(LogType.LogPos);

        public static LoggerWrapper Printer { get; set; } = new LoggerWrapper(LogType.LogPrinter);

        public static LoggerWrapper Update { get; set; } = new LoggerWrapper(LogType.LogUpdate);

        public static LoggerWrapper Maintenance { get; set; } = new LoggerWrapper(LogType.LogMaintenance);

        #endregion

        #region 日志格式化输出

        public static void DebugFormat(LogType type, string format, params object[] args)
        {
            LoggerMaker.WriteLog(type, LogLevel.Debug, string.Format(format, args));
        }

        public static void InfoFormat(LogType type, string format, params object[] args)
        {
            LoggerMaker.WriteLog(type, LogLevel.Info, string.Format(format, args));
        }

        public static void WarnFormat(LogType type, string format, params object[] args)
        {
            LoggerMaker.WriteLog(type, LogLevel.Warn, string.Format(format, args));
        }

        public static void ErrorFormat(LogType type, string format, params object[] args)
        {
            LoggerMaker.WriteLog(type, LogLevel.Error, string.Format(format, args));
        }

        public static void FatalFormat(LogType type, string format, params object[] args)
        {
            LoggerMaker.WriteLog(type, LogLevel.Fatal, string.Format(format, args));
        }

        #endregion

        /// <summary>
        /// 记日志和代码位置;
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type"></param>
        /// <param name="level"></param>
        /// <param name="memberName"></param>
        /// <param name="sourceFilePath"></param>
        /// <param name="sourceLineNumber"></param>
        public static void LogWithPosition(string message, LogType type = LogType.LogMain, LogLevel level = LogLevel.Debug,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            LoggerMaker.WriteLog(type, level, $"#{sourceFilePath}#{memberName}#{sourceLineNumber}#：{message}");
        }
    }

    public class LoggerWrapper
    {
        private readonly LogType _logType;

        public LoggerWrapper(LogType ltype)
        {
            _logType = ltype;
        }

        public void Info(string message)
        {
            LoggerMaker.WriteLog(_logType, LogLevel.Info, message);
        }

        public void Debug(string message)
        {
            LoggerMaker.WriteLog(_logType, LogLevel.Info, message);
        }

        public void Warn(string message)
        {
            LoggerMaker.WriteLog(_logType, LogLevel.Warn, message);
        }

        public void Error(string message)
        {
            LoggerMaker.WriteLog(_logType, LogLevel.Error, message);
        }

        public void Fatal(string message)
        {
            LoggerMaker.WriteLog(_logType, LogLevel.Fatal, message);
        }
    }

    public enum LogLevel
    {
        Debug,
        Info,
        Warn,
        Error,
        Fatal
    }

    public enum LogType
    {
        LogUpdate,
        LogMain,
        LogNet,
        LogDevice,
        LogCash,
        LogPos,
        LogPrinter,
        LogMaintenance,
    }
}